<?php
class ThemeController extends CpController
{
    public function init(){
        $url = new Url();
        $this->breadcrumbs[] = array('name'=>'QT Theme','link'=>$url->createUrl("theme/index"),'class'=>'');
    }
    public function actionIndex(){
        $row_per_page = 10;
        $this->breadcrumbs[] = array('name'=>'DS Theme','link'=>'#','class'=>'active');
        $theme_name = isset($_GET["theme_name"]) ? trim($_GET["theme_name"]):"";
        $theme_path = isset($_GET["theme_path"]) ? trim($_GET["theme_path"]):"";
        $page = isset($_GET["page"]) ? intval($_GET["pgae"]):1;
        list($max_page,$count,$data) = AShopTheme::getDataTheme($theme_name,$theme_path,$page,$row_per_page);
        $this->render("index",array("data"=>$data,"count"=>$count,"max_page"=>$max_page,
        "theme_name"=>$theme_name,"theme_path"=>$theme_path,"page"=>$page));
    }
    public function actionAjaxSaveTheme(){
        $time_now = time();
        $create_user = Yii::app()->user->name;
        $theme_name = isset($_POST["theme_name"]) ? trim(strip_tags($_POST["theme_name"])):"";
        $theme_path = isset($_POST["theme_path"]) ? trim(strip_tags($_POST["theme_path"])):"";
        if($theme_name ==""){
            echo "Tên Theme không được để trống";exit;
        }
        if($theme_path == ""){
            echo "Path Theme không được để trống";exit;
        }

        $data = array(
        "theme_name"=>array("value"=>$theme_name,"type"=>2),
        "theme_path"=>array("value"=>$theme_path,"type"=>2),
        "date_create"=>array("value"=>$time_now,"type"=>1),
        "user_create"=>array("value"=>$create_user,"type"=>2),
        );
        $table = "c_shop_themes";
        $last_id = CommonDB::insertRow($table,$data);
        if($last_id >0){
            $act = 1;
            $obj = 16;
            $content = LogConfig::$action[$act].' '.LogConfig::$object[$obj].': {id: '.$last_id.',name: '.$theme_name.',theme_path: '.$theme_path.'}';
            $result = AAdminLog::insertAdminLog($act,$obj,$content);
            echo 1;exit();
        }else{
            echo "Có vấn đề khi thêm dữ liệu!Hãy thử lại";
        }
    }
    public function actionAjaxQuickUpdate(){
        $time_now = time();
        $user_modify = Yii::app()->user->name;
        $theme_name = isset($_POST["theme_name"]) ? trim(strip_tags($_POST["theme_name"])):"";
        $theme_path = isset($_POST["theme_path"]) ? trim(strip_tags($_POST["theme_path"])):"";
        $id = isset($_POST["id"]) ? intval($_POST["id"]):0;
        if($id >0){
            if($theme_name ==""){
                echo "Tên Theme không được để trống";exit;
            }
            if($theme_path == ""){
                echo "Path Theme không được để trống";exit;
            }
            $data_old = AShopTheme::getDataById($id);
            $data = array(
            "theme_name"=>array("value"=>$theme_name,"type"=>2),
            "theme_path"=>array("value"=>$theme_path,"type"=>2),
            "date_modify"=>array("value"=>$time_now,"type"=>1),
            "user_modify"=>array("value"=>$user_modify,"type"=>2),
            "id"=>array("value"=>$id,"type"=>1)
            );
            $table = "c_shop_themes";
            $result = CommonDB::updateRow($table,$data,array("id"));
            if($result >0){
                $act = 2;
                $obj = 16;
                $content = LogConfig::$action[$act].' '.LogConfig::$object[$obj].': {id: '.$id.',theme_name: '.$data_old["theme_name"].',theme_path: '.$data_old["theme_path"].'} thành';
                $content .= '{id: '.$id.',theme_name: '.$theme_name.',theme_path: '.$theme_path.'}';
                $result = AAdminLog::insertAdminLog($act,$obj,$content);
                echo 1;exit();
            }else{
                echo "Chưa thay đổi dữ liệu";
            }
        }
    }
    public function actionAjaxDelete(){
        $id = isset($_POST["id"]) ? intval($_POST["id"]):0;
        if($id >0){
            $data_old = AShopTheme::getDataById($id);            
            $table = "c_shop_themes";
            $result = CommonDB::deleteRow($table,array("id"=>$id));
            if($result >0){
                $act = 3;
                $obj = 16;
                $content = LogConfig::$action[$act].' '.LogConfig::$object[$obj].': {id: '.$id.',name: '.$data_old["theme_name"].',theme_path: '.$data_old["theme_path"].'}';
                $result = AAdminLog::insertAdminLog($act,$obj,$content);
                echo 1;exit();
            }else{
                echo "Không tồn tại theme này";
            }
        }
    }
}
